package org.androidpn.server.xmpp.packet;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.server.entity.Role;
import org.jivesoftware.smack.util.StringUtils;
import org.xmpp.packet.IQ;

/**
 * packet role
 * 
 * @author 蔡琪
 * 
 */
public class RolePacket extends IQ {

	/**
	 * 角色 操作信息枚举
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum Operation {
		/** 检查nickname */
		checkNickname,
		/** 添加角色 */
		addRole,
		/** 获取当前用户所有的角色信息 */
		getMyRoles,
		/** 登陆角色 */
		loginRole,
		/** 游戏准备 */
		gameReady,
		/** 取消游戏 */
		cancelGameReady
	}

	/** 角色集合 */
	private List<Role> roles = new ArrayList<Role>();
	/** 角色信息 */
	private Role role;
	/** 操作信息 */
	private Operation operation;

	private String result;

	public RolePacket() {
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getChildElementXML() {
		StringBuffer xml = new StringBuffer();
		xml.append("<query xmlns=\"jabber:iq:role\">");
		xml.append("<roles>");
		for (Role role : roles) {
			xml.append(createRoleNode(role));
		}
		xml.append("</roles>");
		if (role != null) {
			xml.append(createRoleNode(role));
		}
		if (operation != null && !operation.equals("")) {
			xml.append("<operation>");
			xml.append(operation);
			xml.append("</operation>");
		}
		if (result != null) {
			xml.append("<result>");
			xml.append(result);
			xml.append("</result>");
		}
		xml.append("</query>");
		return xml.toString().replaceAll("\\>\\s*|\t|\r|\n", ">");
	}

	/**
	 * 生成role节点
	 * 
	 * @param role
	 * @return
	 */
	private String createRoleNode(Role role) {
		return "<role>" + StringUtils.escapeNode(role.toXML()) + ("</role>");
	}
}
